package com.wunu.zn.api.lock.action;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wunu.zn.api.lock.model.RoomEntity;

import common.AppConst;
import common.plugin.mybatis.Db;
import common.support.MobileParam;

@Controller
@RequestMapping("lock/room")
public class RoomAction {
	/**
	 * 我的房间列表
	 * @param param
	 * @return
	 */
	@RequestMapping("/rooms")
	@ResponseBody
	public String activities(MobileParam param) {
		String operator = param.getPara("owner");
		Integer nextId = param.getParaToInt("nextId", 0);
		Integer pageSize = param.getParaToInt("pageSize", AppConst.PAGE_SIZE);
		List<Map<String, Object>> rooms = RoomEntity.findRoomList(operator, nextId, pageSize);
		if (!rooms.isEmpty() && !rooms.isEmpty()) {
			param.put("nextId", rooms.get(rooms.size() - 1).get("id"));
			param.put("pageSize", pageSize);
			param.put("rooms", rooms);

			return param.renderSuccess("1", "OK");
		} else {
			return param.renderFail("0", "Empty");
		}
	}
	/**
	 * 预定房源
	 * @param param
	 * @return
	 */
	@RequestMapping("/reserve")
	@ResponseBody
	public String reserve(MobileParam param) {
		String userId = param.getPara("userId");
		Integer roomId = param.getParaToInt("roomId");
		Integer result = 1;
		RoomEntity room = RoomEntity.findRoomById(roomId);
	    if(room!=null){
	    	room.set("status", 1);
	    	room.set("userId", userId);
	    	result = Db.update(room);
	    	if(result!=1){
	    		return param.renderSuccess("0","NO");
	    	}
	    	return param.renderSuccess("1","OK"); 
	    	
	    }else{
	    	return param.renderFail("0", "NO");
	    }
	}
}
